package com.aaa.dao.impl;

import com.aaa.dao.BaseDao;
import com.aaa.dao.GoodsDao;
import com.aaa.entity.Goods;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

public class GoodsDaoImpl implements GoodsDao {

    BaseDao baseDao = BaseDao.getInstance();

    @Override
    public int addGoods(Goods goods) {
        String sql = "insert into goods (goodsId,name,code,status,type,unitId,price,categoryId) values(?,?,?,?,?,?,?,?)";
        Object[] objects = {goods.getGoodsId(),goods.getName(),goods.getCode(),goods.getStatus(),goods.getType(),goods.getCategoryId(),goods.getPrice(),goods.getCategoryId()};
        return  baseDao.executeUpdate(sql,objects);

    }

    @Override
    public int updateGoods(Goods goods) {
        String sql = "update goods set name=?,code=?,unitId=?,type=?,price=?,categoryId=? where goodsId=?";
        Object[] objects = {goods.getName(),goods.getCode(),goods.getUnitId(),goods.getType(),goods.getPrice(),goods.getCategoryId(),goods.getGoodsId()};
        return baseDao.executeUpdate(sql,objects);
    }

    @Override
    public int changeStatus(Goods goods) {
        String sql = "update goods set status=? where goodsId=?";
        Object[] objects = {goods.getStatus(),goods.getGoodsId()};
        return baseDao.executeUpdate(sql,objects);
    }

    @Override
    public List<Goods> maxGoodsId() {
        String sql = "select max(goodsId)+1 as goodsId from goods";
        return baseDao.query(sql,null,Goods.class);
    }


    @Override
    public  List<Goods> consumerGoodList(Integer pageNumber, Integer pageSize, String searchGoods, String searchName) throws Exception {
        String sql = "select " +
                " g.*,ca.name categoryName,un.name unitName " +
                " from " +
                " goods g LEFT JOIN category ca ON g.categoryId=ca.id LEFT JOIN unit un on g.unitId=un.id " +
                "where 1=1 ";
        if (StringUtils.isNotBlank(searchGoods)) {
            sql += " and g.goodsId = " + searchGoods;
        }
        if (StringUtils.isNotBlank(searchName)) {
            sql += " and g.name like '%" + searchName+"%'";
        }
        sql += " order by g.goodsId desc limit ?,?";
        Object[] params = {pageNumber, pageSize};
        List<Goods> list = baseDao.query(sql, params, Goods.class);
        return list;
    }

    @Override
    public int getConsumerGoodListCount(String searchGoods, String searchName) {
        String sql = "select count(1) len from goods where 1=1";
        if (StringUtils.isNotBlank(searchGoods)) {
            sql += " and goodsId = " + searchGoods;
        }
        if (StringUtils.isNotBlank(searchName)) {
            sql += " and name like '%" + searchName+"%'";
        }
        List<Map<String, Object>> maps = baseDao.query(sql, null);
        if (maps != null && maps.size() > 0) {
            Map<String, Object> map = maps.get(0);
            Integer res = Integer.parseInt(map.get("len") + "");
            return res;
        }
        return 0;
    }

		/**
		 * 销售统计
		 * @return
		 */
		@Override
		public List<Map<String, Object>> getOrderForGoodsSensus() {
        String sql = "select `name`,sum(goodsNumber) `value` from goods join orderinfo on goods.goodsId=orderinfo.goodsId group by `name`";
        Object[] obj= {};
        List<Map<String, Object>> mapList = baseDao.query(sql, obj);
        return mapList;
    }

		/**
         * 1.查出当前商品号库存量
         * 2.减去购买的库存量
         * 3.将减去购买的数量后的库存量重新放入数据库中
         */
    @Override
    public int consumerGoods(Integer goodsId, Integer code) {
        String sql1 = "update goods set code = code -? where id = ?";
        Object[] obj= {code,goodsId};
        int res = baseDao.executeUpdate(sql1,obj);
        return res;
    }

    @Override
    public List<Goods> getConsumerGoodsList(Integer pageNumber, Integer pageSize, String searchGoods, String searchName) {
        String sql = "select " +
                " g.*,ca.name categoryName,un.name unitName " +
                " from " +
                " goods g LEFT JOIN category ca ON g.categoryId=ca.id LEFT JOIN unit un on g.unitId=un.id " +
                "where 1=1 ";
        if (StringUtils.isNotBlank(searchGoods)) {
            sql += " and g.goodsId = " + searchGoods;
        }
        if (StringUtils.isNotBlank(searchName)) {
            sql += " and g.name like '%" + searchName+"%'";
        }
        sql += " order by g.goodsId desc limit ?,?";
        Object[] params = {pageNumber, pageSize};
        List<Goods> list = baseDao.query(sql, params, Goods.class);
        return list;
    }

    @Override
    public int getConsumerGoodsInfoCount(String searchGoods, String searchName) {
        String sql = "select count(1) len from goods where 1=1";
        if (StringUtils.isNotBlank(searchGoods)) {
            sql += " and goodsId = " + searchGoods;
        }
        if (StringUtils.isNotBlank(searchName)) {
            searchName = "%" + searchName + "%";
            sql += " and name like '" + searchName + "'";
        }
        List<Map<String, Object>> maps = baseDao.query(sql, null);
        if (maps != null && maps.size() > 0) {
            Map<String, Object> map = maps.get(0);
            Integer res = Integer.parseInt(map.get("len") + "");
            return res;
        }
        return 0;
    }

}